/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trunk.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import trunk.control.ChatControlador;

/**
 *
 * @author yfoliveira
 */
public class EmoticonView extends JFrame implements ActionListener{
    
    private ArrayList <ImageIcon> imageIcon;
    private ArrayList <JButton> buttons = new ArrayList<JButton>();
    private final JButton okButton;

   
    private final JButton cancelButton;
    ImageIcon iconEscolhido; 
    JLabel icon;
    

    
    public EmoticonView(ArrayList <ImageIcon> imageIcon, ChatControlador chatControlador) {
        super("Emoticons");                
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setResizable(false);
        setSize( 400, 260 );
        
        this.imageIcon=imageIcon;        
        
        
        for (int i = 0; i < this.imageIcon.size(); i++) {            
            
            JButton buttonTemp = new JButton();
            buttonTemp.setIcon(imageIcon.get(i));
                  buttons.add(buttonTemp);  
       }
        
        JPanel emoticonPanel = new JPanel();
       
        for (int i = 0; i < buttons.size(); i++) {
            
                buttons.get(i).addActionListener(this);
                emoticonPanel.add(buttons.get(i));               
        }
        
        
        JScrollPane scrollPane = new JScrollPane(emoticonPanel);
     
        add(scrollPane,BorderLayout.NORTH);
       
        
        okButton = new JButton("OK");
        okButton.addActionListener(chatControlador);
        
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(chatControlador);
        JPanel buttonPanel = new JPanel();
        
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        
        add(buttonPanel,BorderLayout.SOUTH);
      
     
         icon = new JLabel();
       
        add( new JPanel().add(icon));
        setVisible(true);
}

    
    @Override
    public void actionPerformed(ActionEvent ae) {
                     
                     setEnabledAllButton();
                     
                     JButton b = (JButton) ae.getSource();
                     iconEscolhido = (ImageIcon) b.getIcon();
                     
                     icon.setText("Você escolheu o "+iconEscolhido.getDescription());                     
                     
                     b.setEnabled(false);                  
}

    
    private void setEnabledAllButton() {
        
        for (int i = 0; i < buttons.size(); i++) {
            buttons.get(i).setEnabled(true);
        }
        
    }
    
    public ImageIcon getIconEscolhido(){
                return iconEscolhido;
    }
    
     public JButton getOkButton() {
        return okButton;
    }

    public JButton getCancelButton() {
        return cancelButton;
    }
}
